<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
      <el-form-item prop="storageId">
        <el-select v-model="queryParams.storageId" placeholder="请选择入库仓库" clearable>
          <el-option v-for="item in storages" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item prop="inStockOrder">
        <el-input v-model="queryParams.inStockOrder" placeholder="请输入入库单号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item prop="states">
        <el-select v-model="queryParams.states" placeholder="请选择入库状态" clearable>
          <el-option v-for="dict in dict.type.in_storage_status" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="入库人" prop="ingoingPerson">
        <el-input v-model="queryParams.ingoingPerson" placeholder="请输入入库人" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item> -->
      <el-form-item prop="ingoingTime">
        <el-date-picker v-model="queryParams.ingoingTime" clearable type="date" value-format="yyyy-MM-dd" placeholder="请选择入库时间"/>
      </el-form-item>
      <!--<el-form-item label="备注" prop="remarks">
        <el-input v-model="queryParams.remarks" placeholder="请输入备注" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">创建</el-button>
        <!--<el-button type="primary" icon="el-icon-plus" size="mini" v-hasPermi="['storage:goodsInStorage:add']" @click="handleAdd">创建</el-button>-->
        <!--<el-button type="warning" icon="el-icon-download" size="mini" v-hasPermi="['storage:goodsInStorage:export']" @click="handleExport">导出</el-button>-->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="goodsInStorageList">
      <el-table-column label="序号" width="80" align="center">
        <template slot-scope="{$index}">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="仓库名称" align="left" header-align="center" prop="storageName" />
      <el-table-column label="入库单号" align="center" prop="inStockOrder" />
      <el-table-column label="入库状态" align="center" prop="states">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.in_storage_status" :value="scope.row.states" />
        </template>
      </el-table-column>
      <el-table-column label="入库人" align="center" prop="ingoingPerson" />
      <el-table-column label="入库时间" align="center" prop="ingoingTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ingoingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="修改人" align="center" prop="updateBy" />
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180" />
      <el-table-column label="操作" align="center" class-name="small-padding" width="300">
        <template slot-scope="scope">
          <el-button v-hasPermi="['storage:goodsInStorage:query']" size="mini" type="primary" @click="handleView(scope.row)">详情</el-button>
          <template v-if="scope.row.states===0">
            <el-button v-hasPermi="['storage:goodsInStorage:edit']" size="mini" type="primary" @click="handleUpdate(scope.row)">修改</el-button>
            <el-button v-hasPermi="['storage:goodsInStorage:edit']" size="mini" type="primary" @click="handleStorageGoodsIn(scope.row)">入库</el-button>
            <el-button v-hasPermi="['storage:goodsInStorage:remove']" size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!--添加或修改商品入库对话框-->
    <storage-goods-list :id="id" :title="title" :visible.sync="open" type="in" :mode="mode" @ok="onOk" />
  </div>
</template>

<script>
import { listGoodsInStorage, getGoodsInStorage, delGoodsInStorage, updateInOfStorage } from '@/api/storage/goodsInStorage'
import { listAllStorage } from '@/api/storage/storage'
import { StorageGoodsList } from '../components/StorageGoodsList.vue'

export default {
  name: 'GoodsInStorage',
  dicts: ['in_storage_status'],
  components: {
    StorageGoodsList
  },
  data() {
    return {
      // 按钮loading
      // buttonLoading: false,
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品入库表格数据
      goodsInStorageList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storageId: undefined,
        inStockOrder: undefined,
        states: undefined,
	      ingoingPerson: undefined,
        ingoingTime: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      // rules: {},
      storages: [],
      mode: null,
      id: ''
    }
  },
  created() {
    this.getList()
    this.listAllStorage()
  },
  methods: {
    listAllStorage() {
      listAllStorage().then(({ data }) => {
        this.storages = data
      })
    },
    /** 查询商品入库列表 */
    getList() {
      this.loading = true
      listGoodsInStorage(this.queryParams).then(response => {
        this.goodsInStorageList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        storageId: undefined,
        inStockOrder: undefined,
        states: undefined,
        ingoingPerson: undefined,
        ingoingTime: undefined,
        remarks: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      }
      this.resetForm('form')
      this.id = ''
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    async handleAdd() {
      this.mode = 'add'
      this.reset()
      this.open = true
      this.title = '创建入库单'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.mode = 'update'
      this.loading = true
      this.reset()
      this.id = row.id
      this.open = true
      this.title = '编辑入库单'
      getGoodsInStorage(id).then(response => {
        this.loading = false
        this.form = response.data
      })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id
      this.$modal.confirm('是否确认删除？').then(() => {
        this.loading = true
        return delGoodsInStorage(id)
      }).then(() => {
        this.loading = false
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      }).finally(() => {
        this.loading = false
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('storage/goodsInStorage/export', {
        ...this.queryParams
      }, `goodsInStorage_${new Date().getTime()}.xlsx`)
    },
    onOk() {
      if (this.mode !== 'view') {
        this.getList()
      }
    },
    handleStorageGoodsIn(row) {
      this.$modal.confirm('确定入库吗？').then(() => {
        this.loading = true
        return updateInOfStorage({ ...row })
      }).then(() => {
        this.loading = false
        this.getList()
        this.$modal.msgSuccess('入库成功')
      }).catch(() => {
      }).finally(() => {
        this.loading = false
      })
    },
    handleView(row) {
      this.mode = 'view'
      this.reset()
      this.id = row.id
      this.open = true
      this.title = '入库单详情'
    }
  }
}
</script>
